package org.s63.kaptcha;

import com.google.code.kaptcha.Constants;
import com.google.code.kaptcha.Producer;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.servlet.KaptchaServlet;
import com.google.code.kaptcha.util.Config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

import java.util.Properties;

@Configuration
@ConditionalOnClass(KaptchaServlet.class)
@Import(KaptchaProperites.class)
public class KaptchaConfiguration {
    @Autowired
    private KaptchaProperites kaptchaProperites;

    @Bean
    public Producer producer() {
        Properties properties = new Properties();
        properties.setProperty(Constants.KAPTCHA_IMAGE_WIDTH, "" + kaptchaProperites.getImageWidth());
        properties.setProperty(Constants.KAPTCHA_IMAGE_HEIGHT, "" + kaptchaProperites.getImageHeight());
        properties.setProperty(Constants.KAPTCHA_TEXTPRODUCER_FONT_SIZE, "" + kaptchaProperites.getFontSize());
        properties.setProperty(Constants.KAPTCHA_TEXTPRODUCER_FONT_COLOR, "" + kaptchaProperites.getFontColor());
        properties.setProperty(Constants.KAPTCHA_TEXTPRODUCER_CHAR_STRING, "" + kaptchaProperites.getCharString());
        properties.setProperty(Constants.KAPTCHA_TEXTPRODUCER_CHAR_LENGTH, "" + kaptchaProperites.getCharLength());
        properties.setProperty(Constants.KAPTCHA_NOISE_IMPL, "" + kaptchaProperites.getNoiseImpl());

        DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
        defaultKaptcha.setConfig(new Config(properties));
        return defaultKaptcha;
    }
}
